package org.fude.bootstrap.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.fude.dao.AchievementMapper;
import org.fude.dao.MenuMapper;
import org.fude.dao.UserMapper;
import org.fude.entity.Menu;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class PageFowardController {
	
	private final static Logger logger = LoggerFactory.getLogger(PageFowardController.class);
	@Autowired
	UserMapper userMapper;
	@Autowired
	AchievementMapper achieveMapper;
	@Autowired
	MenuMapper menuMapper;
	@RequestMapping("/main")
	String hd(HttpServletRequest request) {
		return "init";
	}
	
	/*@RequestMapping("pages/homePage/homePage.html")
	String pages(ModelMap map,  @PathVariable("pkg") String pkg, @PathVariable("path") String path) {
			
		return "pages/homePage/homePage";
	}*/
	
	@RequestMapping("pages/{pkg}/{path}.html")
	String pageHome1(ModelMap map,  @PathVariable("pkg") String pkg, @PathVariable("path") String path) {
		if("homePage".equals(pkg) && "homePage".equals(path)) {
			List<Menu> list = menuMapper.getHomePageList();
			if(list == null) {
				list = new ArrayList<>();
			}
			map.addAttribute("menuList", list);
		}
		
		return "pages/" + pkg + "/" + path;
	}
	
	@RequestMapping("pages/{pkg}/{path1}/{path2}.html")
	String pagesInner(ModelMap map,  @PathVariable("pkg") String pkg,
			@PathVariable("path1") String path1, @PathVariable("path2") String path2) {
		return String.format("pages/%s/%s/%s", pkg, path1, path2);
	}
	
	@RequestMapping("pages/user/userCount.html")
	String pagesUserCount(ModelMap map) {
		int total = userMapper.countTotal();
		int totalMoney = achieveMapper.countTotalMoney();
		map.put("totalPeople", total);
		map.put("totalMoney", totalMoney);
		return "pages/user/userCount";
	}
	
}
